<?php
/**
 * Created by PhpStorm.
 * User: liuwei
 * Date: 2017/5/15
 * Time: 下午3:35
 */

function getIP() {
    if (getenv("HTTP_CLIENT_IP")) {
        return getenv("HTTP_CLIENT_IP");
    } else if(getenv("HTTP_X_FORWARDED_FOR")) {
        return getenv("HTTP_X_FORWARDED_FOR");
    } else if(getenv("REMOTE_ADDR")){
        return getenv("REMOTE_ADDR");
    } else{
        return "Unknow";
    }
}

function getUniName() {
    return md5(uniqid(microtime(true),true));
}

function buildRandomString($type=1,$length=4) {
    if ($type == 1) {
        $chars = join ( "", range ( 0, 9 ) );
    } elseif ($type == 2) {
        $chars = join ( "", array_merge ( range ( "a", "z" ), range ( "A", "Z" ) ) );
    } elseif ($type == 3) {
        $chars = join ( "", array_merge ( range ( "a", "z" ), range ( "A", "Z" ), range ( 0, 9 ) ) );
    }

    if ($length > strlen ( $chars )) {
        exit ( "字符串长度不够" );
    }
    $chars = str_shuffle ( $chars );
    return substr ( $chars, 0, $length );
}

function cleanJson($str) {
    return stripslashes($str);//将字符串进行处理
}
